using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using AudioDjStudio;

namespace IntelliTouchMix
{
	public partial class FormAudioOptions : Form
	{
		public FormAudioOptions()
		{
			InitializeComponent();
		}

		public FormMain frmMain;

		private void FormAudioOptions_Load(object sender, EventArgs e)
		{
			int nOutputDevices = frmMain.audioDjStudio1.GetOutputDevicesCount();
			for (short i = 0; i < nOutputDevices; i++)
			{
				ComboMasterDevices.Items.Add(frmMain.audioDjStudio1.GetOutputDeviceDesc(i));
				ComboMonitorDevices.Items.Add(frmMain.audioDjStudio1.GetOutputDeviceDesc(i));
			}
			ComboMasterDevices.SelectedIndex = frmMain.m_selOutputMaster;
			ComboMonitorDevices.SelectedIndex = frmMain.m_selOutputMonitor;

			TextLatency.Text = frmMain.audioDjStudio1.BufferLength.ToString();

			Int32 nScrollingWaveformViewInMs = frmMain.m_nScrollingWaveformViewInMs;
			TextWaveformView.Text = nScrollingWaveformViewInMs.ToString();
		}

		private void BtnOK_Click(object sender, EventArgs e)
		{
			if (ComboMasterDevices.SelectedIndex == ComboMonitorDevices.SelectedIndex &&
				ComboMasterSpeakers.SelectedIndex == ComboMonitorSpeakers.SelectedIndex)
			{
				DialogResult result;
				result = MessageBox.Show("Warning: This demo gives its best when dealing with two different output " +
									"device/speaker pair; you have chosen the same output device/speaker pair for Master and Monitor " +
									"so you want be able to use the Monitor output with your headphones: do you want to select another " +
									"sound card for your Monitor output?", "Same output device", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
				if (result == DialogResult.Yes)
					// don't close the form
					return;

				frmMain.BtnOutputMaster1.Enabled = false;
				frmMain.BtnOutputMaster2.Enabled = false;
				frmMain.BtnOutputMonitor1.Enabled = false;
				frmMain.BtnOutputMonitor2.Enabled = false;
				MessageBox.Show("Monitor switch disabled", "Same output device", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			else
			{
				frmMain.BtnOutputMaster1.Enabled = true;
				frmMain.BtnOutputMaster2.Enabled = true;
				frmMain.BtnOutputMonitor1.Enabled = true;
				frmMain.BtnOutputMonitor2.Enabled = true;
			}

			frmMain.m_selOutputMaster = (Int16) ComboMasterDevices.SelectedIndex;
			frmMain.m_selOutputMonitor = (Int16) ComboMonitorDevices.SelectedIndex;

			frmMain.m_selSpeakersMaster = (Int16) ComboMasterSpeakers.SelectedIndex;
			frmMain.m_selSpeakersMonitor = (Int16) ComboMonitorSpeakers.SelectedIndex;

			frmMain.audioDjStudio1.BufferLength = Convert.ToInt16 (TextLatency.Text);
			frmMain.m_nScrollingWaveformViewInMs = Convert.ToInt32 (TextWaveformView.Text);
			if (frmMain.m_nScrollingWaveformViewInMs < 500)
				frmMain.m_nScrollingWaveformViewInMs = 500;

			Close();
		}

		private void ComboMasterDevices_SelectedIndexChanged(object sender, EventArgs e)
		{
			ComboMasterSpeakers.Items.Clear();

			int nSpeakers = frmMain.audioDjStudio1.GetOutputSpeakersCount((Int16)ComboMasterDevices.SelectedIndex);

			// enable stereo speakers
			if (nSpeakers >= 8)
			{
				// 7.1 device
				ComboMasterSpeakers.Items.Add("Front");
				ComboMasterSpeakers.Items.Add("Rear");
				ComboMasterSpeakers.Items.Add("Center");
				ComboMasterSpeakers.Items.Add("Rear Center");
			}
			else if (nSpeakers >= 6)
			{
				// 5.1 device
				ComboMasterSpeakers.Items.Add("Front");
				ComboMasterSpeakers.Items.Add("Rear");
				ComboMasterSpeakers.Items.Add("Center");
			}
			else if (nSpeakers >= 4)
			{
				// Quadraphonic device
				ComboMasterSpeakers.Items.Add("Front");
				ComboMasterSpeakers.Items.Add("Rear");
			}
			else
				// stereo device
				ComboMasterSpeakers.Items.Add("Front");

			ComboMasterSpeakers.SelectedIndex = frmMain.m_selSpeakersMaster;
		}

		private void ComboMonitorDevices_SelectedIndexChanged(object sender, EventArgs e)
		{
			ComboMonitorSpeakers.Items.Clear();

			int nSpeakers = frmMain.audioDjStudio1.GetOutputSpeakersCount((Int16)ComboMonitorDevices.SelectedIndex);

			// enable stereo speakers
			if (nSpeakers >= 8)
			{
				// 7.1 device
				ComboMonitorSpeakers.Items.Add("Front");
				ComboMonitorSpeakers.Items.Add("Rear");
				ComboMonitorSpeakers.Items.Add("Center");
				ComboMonitorSpeakers.Items.Add("Rear Center");
			}
			else if (nSpeakers >= 6)
			{
				// 5.1 device
				ComboMonitorSpeakers.Items.Add("Front");
				ComboMonitorSpeakers.Items.Add("Rear");
				ComboMonitorSpeakers.Items.Add("Center");
			}
			else if (nSpeakers >= 4)
			{
				// Quadraphonic device
				ComboMonitorSpeakers.Items.Add("Front");
				ComboMonitorSpeakers.Items.Add("Rear");
			}
			else
				// stereo device
				ComboMonitorSpeakers.Items.Add("Front");

			ComboMonitorSpeakers.SelectedIndex = frmMain.m_selSpeakersMonitor;
		}
	}
}